// miniprogram/pages/newmain_t/newmain_t.js
Page({
  _showMore: function(e){
    wx.navigateTo({
      url: '/pages/navigation/navigation?type=' + e.currentTarget.dataset.type
    })
  },
  _view: function(e){
    console.log('点击, e=', e)
    if(e.currentTarget.dataset.type == 'article'){
      wx.navigateTo({
        url: '/pages/wenzhangye/wenzhangye?id=' + e.currentTarget.id
      })
    }else{
      wx.navigateTo({
        url: '/pages/shipinye/shipinye?id=' + e.currentTarget.id
      })
    }
  },
  /** 
   * 页面的初始数据
   */
  data: {
    hots: [], //{rid, image, title}
    knowledge: [],
    edu: [],
    law: [],
    history: [],
    work: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // return undefined
    wx.cloud.database().collection('newmain_test').field({
      rid: true,
      class: true,
      isHot: true
    }).get().then(res => {
      for(let i = 0, l = res.data.length; i < l; i++){
        //是热点新闻
        let rid = res.data[i].rid
        if(res.data[i].isHot){
          wx.cloud.database().collection('resources').where({_id: rid})
          .field({title:true, image: true, type: true}).get().then(res =>{
            let image = res.data[0]['image'] || 'bsgjmm.jpg'
            this.setData({hots: this.data.hots.concat([{'rid': rid, 'title': res.data[0].title, 'image': image, type: res.data[0].type}])})
          }).catch(err => {console.log(err)})
        }else{
          let c = res.data[i].class //这条数据对应的目录
          wx.cloud.database().collection('resources').where({_id: rid})
          .field({title: true, editor: true, type: true, source: true, date: true}).get()
          .then(res => {
            let source = res.data[0]['source'] || ''
            let time = res.data[0].date
            let date = time = "" + time.getFullYear().toString() + "-" + (time.getMonth()+1).toString() + "-" + time.getDate().toString() + ' ' + time.getHours().toString() + ":" + time.getMinutes().toString()
            let obj = [{'rid': rid, 'title': res.data[0].title, 'editor': res.data[0].editor, 'source': source, 'type': res.data[0].type, 'date': date}]
            if(c == 'knowledge'){
              this.setData({knowledge: this.data.knowledge.concat(obj)})
            }else if(c == 'edu'){
              this.setData({edu: this.data.edu.concat(obj)})
            }else if(c == 'law'){
              this.setData({law: this.data.law.concat(obj)})
            }else if(c == 'hisotry'){
              this.setData({history: this.data.hisotry.concat(obj)})
            }else if(c == 'work'){
              this.setData({work: this.data.work.concat(obj)})
            }
          }).catch(err => {console.log(err)})
        }
      }
      console.log('遍历完毕后，this.data = ', this.data)
    }).catch(err => {console.log(err)})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})